package cn.lili.modules.grab.dingdong.service.impl;

import cn.lili.common.exception.ServiceException;
import cn.lili.modules.grab.dingdong.DingdongBody;
import cn.lili.modules.grab.dingdong.reps.goods.*;
import cn.lili.modules.grab.dingdong.res.goods.*;
import cn.lili.modules.grab.dingdong.facade.DingdongFacade;
import cn.lili.modules.grab.dingdong.service.DingdongAbsService;
import cn.lili.modules.grab.dingdong.config.DingdongConfig;
import cn.lili.modules.grab.dingdong.base.BaseResult;
import cn.lili.modules.grab.dingdong.facade.DingdongGoodsFacede;
import cn.lili.modules.grab.dingdong.service.DingdongGoodsService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class DingdongGoodsServiceImpl extends DingdongAbsService implements DingdongGoodsService {

    @Resource
    private DingdongFacade dingdongFacade;
    @Resource
    private DingdongConfig dingdongConfig;
    @Resource
    private DingdongGoodsFacede dingdongGoodsFacede;

    /**
     * 商品列表
     *
     * @param goodsQueryRequest 请求内容
     */
    @Override
    public List<GoodsQueryRespose> goodsList(GoodsQueryRequest goodsQueryRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(goodsQueryRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getGoodsListUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<GoodsQueryRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), GoodsQueryRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    /**
     * 商品分类
     *
     * @param goodsCateRequest 请求内容
     */
    @Override
    public List<GoodsCateRespose> goodsCate(GoodsCateRequest goodsCateRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(goodsCateRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getGoodsCateUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            List<GoodsCateRespose> resultList = JSONArray.parseArray(String.valueOf(baseResult.getContent()), GoodsCateRespose.class);
            return resultList;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    /**
     * 商品详情
     *
     * @param detailRequest 请求内容
     */
    @Override
    public DetailRespose detail(DetailRequest detailRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(detailRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getDetailUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            JSONObject parentJson = JSONObject.parseObject(String.valueOf(baseResult.getContent()));
            DetailRespose detailRespose = dingdongGoodsFacede.detailResposeBuilder(parentJson);
            return detailRespose;
        }
        return null;
    }

    /**
     * 商品配送方式
     *
     * @param retYfModeRequest 请求内容
     */
    @Override
    public GetYfModeRespose getYfMode(GetYfModeRequest retYfModeRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(retYfModeRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getGetYfModeUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            JSONObject parentJson = JSONObject.parseObject(String.valueOf(baseResult.getContent()));
            GetYfModeRespose result = dingdongGoodsFacede.getYfModeResposeBuilder(parentJson);
            return result;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    /**
     * 商品品牌列表
     *
     * @param getBrandRequest 请求内容
     */
    @Override
    public GetBrandRespose getBrand(GetBrandRequest getBrandRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(getBrandRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getGetBrandUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            JSONObject parentJson = JSONObject.parseObject(String.valueOf(baseResult.getContent()));
            GetBrandRespose result = dingdongGoodsFacede.getBrandResposeBuilder(parentJson);
            return result;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    /**
     * 商品库存查询
     *
     * @param getGoodsStockRequest 请求内容
     */
    @Override
    public GetGoodsStockRespose getGoodsStock(GetGoodsStockRequest getGoodsStockRequest) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(getGoodsStockRequest);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getGetGoodsStockUrl(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            JSONObject parentJson = JSONObject.parseObject(String.valueOf(baseResult.getContent()));
            String stock = parentJson.getString("stock");
            return GetGoodsStockRespose.builder()
                    .stock(stock)
                    .build();
        }
        throw new ServiceException(baseResult.getMsg());
    }

    /**
     * 新版商品列表接口（带总数）
     *
     * @param goodsListV1Request 请求内容
     */
    @Override
    public GoodsListV1Respose goodsList_v1(GoodsListV1Request goodsListV1Request) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(goodsListV1Request);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getGoodsListV1Url(), JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            JSONObject parentJson = JSONObject.parseObject(String.valueOf(baseResult.getContent()));
            GoodsListV1Respose result = dingdongGoodsFacede.goodsListV1ResposeBuilder(parentJson);
            return result;
        }
        throw new ServiceException(baseResult.getMsg());
    }

    /**
     * 商品库存查询
     *
     * @param getGoodsStockV1Request 请求内容
     */
    @Override
    public GetGoodsStockV1Respose getGoodsStock_v1(GetGoodsStockV1Request getGoodsStockV1Request) {
        //构造请求
        DingdongBody dingdongBody = dingdongFacade.dingdongBodyBuilder(getGoodsStockV1Request);
        //发送请求
        BaseResult baseResult = post(dingdongConfig.getGoodsStockV1Url, JSONObject.toJSONString(dingdongBody), dingdongConfig.getPublic_key());
        //解析返回结果
        if ("200".equals(baseResult.getStatus())) {
            JSONObject parentJson = JSONObject.parseObject(String.valueOf(baseResult.getContent()));
            GetGoodsStockV1Respose result = dingdongGoodsFacede.getGoodsStockV1ResposeBuilder(parentJson);
            return result;
        }
        throw new ServiceException(baseResult.getMsg());
    }
}
